$(function () {
	//console.log("1111");
	$.post("../User/GetAllUsers", null, function (data, textStatus) {

		if (textStatus == "success") {
			console.log(data);
			//var msg = $.parseJSON(data);
			tableData = parseToTableData(data);
			$("#userTable").bootstrapTable({
				columns: [{
						field: 'username',
						title: '真实姓名',
						sortable: true

				}, {
						field: 'permission',
						title: '用户权限',
						sortable: true

				},
					{
						field: 'type',
						title: '用户类型',
						sortable: true
				}, {
						field: 'area',
						title: '用户所在位置',
						sortable: true
				}, {
						field: 'manage',
						title: '操作'
				}],
				radio: true,
				checkbox: true,
				data: tableData,
				striped: true,
				align: 'center',
				halign: 'center',
				valign: 'middle',
				sortable: true,
				sortName: 'userName',
				undefinedText: "",
				pagination: true,
				sidePagination: 'client',
				idField: 'code',
				uniqueId: 'code',
				search: true,
				paginationDetailHAlign: "left",
				local: "zh-CN",
				pageSize: 10,
				onSort: function () {
					$("[name='my-checkbox']").bootstrapSwitch();
				}

			});
		}
	});
});


function refresh() {
	$("[name='my-checkbox']").bootstrapSwitch();
}

function parseToTableData(data) {
	var tableData1 = new Array();
	for (var i in data) {
		var temp = {};
		temp.code = data[i]["code"];
		temp.username = data[i]["username"];
		temp.permission = data[i]["permission"];
		temp.area = data[i]["area"];
		temp.type = data[i]["type"];
		temp.userRole = data[i]["userRole"];
		temp.department = data[i]["department"];
		//测试
		/*data[i]["enable"] = true;
if (data[i]["enable"] == true) {
	temp.enable = '<input type="checkbox"  onChange="enableButton(this);" checked>';
} else {
	temp.enable = '<input type="checkbox"  onChange="enableButton(this);">';
}*/
		temp.manage = '<div class="btn-group" role="group" aria-label="...">' +
			'<button type="button" class="btn btn-default btn-primary" onClick="deleteUser(this);">删除用户</button>' + '<button type="button" class="btn btn-default btn-danger" onClick="editUser(this)">编辑用户</button>' + '</div>';
		tableData1.push(temp);
	}
	return tableData1;
}

function editUser(th) {
	window.location.href = "updateUserPage?code=" + $(th).parents("tr").find("td:first-child").text();
}

function deleteUser(th) {
	var username = $(th).parents("tr").find("td:first-child").text();

	var param = "username=" + username;
	$.post("../User/DeleteUser", param, function (data, status) {
		if (status == "success") {
			alert("修改成功");
			window.location.reload();
		} else {
			alert("修改失败");

		}
	});
	//$("table").bootstrapTable('removeByUniqueId', i);
}

/*function enableButton(th) {
	var code = $(th).parents("tr").find("td:first-child").text();
	var param = "code=" + code;
	param += "&";
	param += "enable=" + $(th).is(":checked");
	$.post("enableUser", param, function (data, textStatus) {
		console.log(data);
		if (textStatus == "success") {
			var data = $.parseJSON(data);
			if (data["status"] == true) {

			} else {

			}
		}

	});
}*/